<!doctype html>
<html>
<head>
<title>URI Stat</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf8" />
    <link href='css/bootstrap.min.css' rel="stylesheet" />
    <link href="css/style.css" type="text/css" rel="stylesheet" />
    <script type="text/javascript" src="js/jquery.min.js"></script>
    <script src="js/lang.js" type="text/javascript" charset="utf8"></script>
    <script src="js/common.js" type="text/javascript" charset="utf8"></script>
</head>
<body>
    <div class="container-fluid">
        <div class="row-fluid">
            <div class="span12">
                <h3 style="display:inline;">
                    Web URI Stat
                    <a href="weburi.json" target="_blank" class="lang"  langKey="ViewJSONAPI"></a>
                    <span style="font-size:50%;font-weight:100;">
                        ${(dubboProvider.toNameString())!}
                    </span>
                </h3>
                <table id="dataTable" class="table table-bordered table-striped responsive-utilities">
                    <thead>
                        <tr>
                            <th>N</th>
                            <th>
                                <a id="th-URI" class="lang" langKey="URI">URI</a>
                            </th>
                            <th>
                                <a id="th-RequestCount" class="lang" langKey="RequestCount">RequestCount</a>
                            </th>
                            <th>
                                <a id="th-RequestTimeMillis" class="lang" langKey="RequestTimeMillis">RequestTimeMillis</a>
                            </th>
                            <th>
                                <a id="th-RunningCount" class="lang" langKey="RunningCount">RunningCount</a>
                            </th>
                            <th>
                                <a id="th-ConcurrentMax" class="lang" langKey="ConcurrentMax">ConcurrentMax</a>
                            </th>
                            <th>
                                <a id="th-JdbcExecuteCount" class="lang" langKey="JdbcExecuteCount">JdbcExecuteCount</a>
                            </th>
                            <th>
                                <a id="th-JdbcExecuteErrorCount" class="lang" langKey="JdbcExecuteErrorCount">JdbcExecuteErrorCount</a>
                            </th>
                            <th>
                                <a id="th-JdbcExecuteTimeMillis" class="lang" langKey="JdbcExecuteTimeMillis">JdbcExecuteTimeMillis</a>
                            </th>
                            <th>
                                <a id="th-JdbcCommitCount" class="lang" langKey="JdbcCommitCount">JdbcCommitCount</a>
                            </th>
                            <th>
                                <a id="th-JdbcRollbackCount" class="lang" langKey="JdbcRollbackCount">JdbcRollbackCount</a>
                            </th>
                            <th>
                                <a id="th-JdbcFetchRowCount" class="lang" langKey="JdbcFetchRowCount">JdbcFetchRowCount</a>
                            </th>
                            <th>
                                <a id="th-JdbcUpdateCount" class="lang" langKey="JdbcUpdateCount">JdbcUpdateCount</a>
                            </th>
                            <th align="left" width="100">
                                <span class="lang" langKey="Histogram">Histogram</span>
                                <br />
                                [
                                <a id="th-Histogram[0]" class="langTitle" langKey="count1ms" title="count of '0-1 ms'">-</a>
                                <a id="th-Histogram[1]" class="langTitle" langKey="count10ms" title="count of '1-10 ms'">-</a>
                                <a id="th-Histogram[2]" class="langTitle" langKey="count100ms" title="count of '10-100 ms'">-</a>
                                <a id="th-Histogram[3]" class="langTitle" langKey="count1s" title="count of '100ms-1 s'">-</a>
                                <a id="th-Histogram[4]" class="langTitle" langKey="count10s" title="count of '1-10 s'">-</a>
                                <a id="th-Histogram[5]" class="langTitle" langKey="count100s" title="count of '10-100 s'">-</a>
                                <a id="th-Histogram[6]" class="langTitle" langKey="count1000s" title="count of '100-1000 s'">-</a>
                                <a id="th-Histogram[7]" class="langTitle" langKey="countBg1000s" title="count of '> 1000 s'">-</a>
                                ]
                            </th>
                        </tr>
                    </thead>
                    <tbody></tbody>
                </table>
            </div>
        </div>
    </div>

    <script type="text/javascript">
        $.namespace("druid.weburi");

        druid.weburi = function() {
            return {
                init : function() {
                    $("#dataTable th a").click(function(obj) {
                        druid.common.setOrderBy(obj.target.id.substring(3))
                    })

                    druid.common.buildHead(5);
                    druid.common.ajaxuri = 'weburi.json?';
                    druid.common.handleCallback = druid.weburi.handleAjaxResult;
                    druid.common.ajaxRequestForBasicInfo();

                    // var intervalId = setInterval("druid.common.ajaxRequestForBasicInfo();", 10000);

                    druid.common.switchInterval = function(){
                        if ( $('a#switchInterval').hasClass('disabled') ) {
                            intervalId = setInterval("druid.common.ajaxRequestForBasicInfo();", 10000);
                            $('a#switchInterval').removeClass('disabled'); 
                        } else {
                            clearInterval(intervalId); 
                            $('a#switchInterval').addClass('disabled'); 
                        }
                    };
                },

                handleAjaxResult : function(data) {
                    var statList = data.Content;
                    if (statList == null)
                        return;

                    var sqlStatTable = document.getElementById("dataTable");
                    while (sqlStatTable.rows.length > 1) {
                        sqlStatTable.deleteRow(1);
                    }

                    var html = "";
                    for (var i = 0; i < statList.length; i++) {
                        var stat = statList[i];
                        var newRow = sqlStatTable.insertRow(-1);
                        html += "<tr>";

                        if (stat.BudoGraph) {
                            html += "<td title=\"点击查看折线图\"><a target=\"_blank\" href=\"line-graph?type=URL&key=" + encodeURI(stat.URI) + "\"><b>" + (i + 1) + "</b></a></td>";
                        } else {
                            html += "<td>" + (i + 1) + "</td>";
                        }

                        var methodHref = "../../#method?method=" + encodeURI(stat.URI) + "&host=" + druid.common.host() + "&port=" + druid.common.port();

                        html += "<td title=\"" + stat.URI + " \">" //
                            + "<a title='查看谁使用了这个方法' target='_blank' href='" + methodHref + "'>+ </a>" // 
                            + '<a target="_blank" href="weburi-detail.html?uri=' + encodeURI(stat.URI) + '">' + druid.common.subUriString(stat.URI, 120) + '</a>' // 
                            + "</td>";

                        html += "<td>" + replace(stat.RequestCount) + "</td>";
                        html += "<td>" + replace(stat.RequestTimeMillis) + "</td>";
                        html += "<td>" + replace(stat.RunningCount) + "</td>";
                        html += "<td>" + replace(stat.ConcurrentMax) + " / <a title=\"修改限流参数\" class=\"SentinelQpsMax\">" + stat.SentinelQpsMax + "</a>" + "</td>";
                        html += "<td>" + replace(stat.JdbcExecuteCount) + "</td>";
                        html += "<td>" + replace(stat.JdbcExecuteErrorCount) + "</td>";
                        html += "<td>" + replace(stat.JdbcExecuteTimeMillis) + "</td>";
                        html += "<td>" + replace(stat.JdbcCommitCount) + "</td>";
                        html += "<td>" + replace(stat.JdbcRollbackCount) + "</td>";
                        html += "<td>" + replace(stat.JdbcFetchRowCount) + "</td>";
                        html += "<td>" + replace(stat.JdbcUpdateCount) + "</td>";
                        html += "<td>" + '[' + stat.Histogram + ']' + "</td>";
                        html += "</tr>";
                    }

                    $("#dataTable tbody").html(html);
                    druid.common.stripes();

                    // 动态限制并发数修改
                    $(".SentinelQpsMax").on("click", function() {
                        var resource = $(this).parent().parent().find("td").eq(1).attr("title");
                        var qpsMax = prompt("QpsMax of \n" + resource,"");
                        $.ajax({ 
                            url: "sentinel/update.api",
                            type: 'POST',
                            data: {
                                "resource" : resource,
                                "qpsMax" : qpsMax
                            }
                        }).then(function(response) {
                            alert(response);
                        });
                    });
                }
            }
        }();

        $(document).ready(function() {
            druid.weburi.init();
        });
    </script>
</body>
</html>